package cn.lee.demo.receiver;

import android.app.Activity;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.RemoteException;
import android.telephony.TelephonyManager;
import android.util.Log;
import com.android.internal.telephony.ITelephony;

import java.lang.reflect.Method;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 12-4-24
 * Time: 下午5:55
 */
public class IncomingCallReceiver extends BroadcastReceiver {
    private static final String TAG="Cloud-Manager";

    private final static String BLOCKED_NUMBER = "15555215554";//要拦截的号码
    //占线时转移，这里13800000000是空号，所以会提示所拨的号码为空号
    private final String ENABLE_SERVICE = "tel:**67*13800000000%23";
    //占线时转移
    private final String DISABLE_SERVICE = "tel:%23%2367%23";

    private ITelephony iTelephony = null;
    private AudioManager mAudioManager = null;
    private TelephonyManager telephonyMgr = null;

    public IncomingCallReceiver() {
        Log.v(TAG,"IncomingCallReceiver start.....");
    }

    private void initManagers(Context context) {
        if (mAudioManager == null) {
            mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        }
        if (telephonyMgr == null) {
            telephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            try {
                Method getITelephonyMethod = TelephonyManager.class.getDeclaredMethod("getITelephony", (Class[]) null);
                getITelephonyMethod.setAccessible(true);
                iTelephony = (ITelephony) getITelephonyMethod.invoke(telephonyMgr, (Object[]) null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v(TAG, "onReceive: calling" );
        initManagers(context);
        String action = intent.getAction();
        Log.v(TAG, "action:" + action);

        if ("android.intent.action.PHONE_STATE".equals(action)) {//拦截电话
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            Log.v(TAG, "State: " + state);

            String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Log.v(TAG, "Incomng Number: " + number);

            if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) {//电话正在响铃
                if (number.equals(BLOCKED_NUMBER)) {//拦截指定的电话号码
                    //先静音处理
                    mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                    Log.v(TAG, "Turn ringtone silent");
                    try {
                        //挂断电话
                        iTelephony.endCall();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //再恢复正常铃声
                    mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                }
            }
        }
//        else if("android.provider.Telephony.SMS_RECEIVED".equals(action)){//拦截短信
//            SmsMessage sms = getMessagesFromIntent(intent)[0];
//            String number = sms.getOriginatingAddress();
//            Log.d(TAG, "Incomng Number: " + number);
//            number = trimSmsNumber("+86", number);//把国家代码去除掉
//            if(number.equals(BLOCKED_NUMBER)){
//                abortBroadcast();//这句很重要，中断广播后，其他要接收短信的应用都没法收到短信广播了
//            }
//        }
    }

}
